using System.Collections;
using System.Collections.Generic;
using QFramework;
using UnityEditor;
using UnityEngine;

public class CoinUpgradeSystem : AbstractSystem,ICanSave
{
    public List<CoinUpgradeItem> Items { get; } = new List<CoinUpgradeItem>();//升级按钮列表

    public static EasyEvent OnCoinUpgradeSystemChanged = new EasyEvent();//注册升级界面刷新时间，当item升级后会调用该事件
    public CoinUpgradeItem Add(CoinUpgradeItem item)
    {
        Items.Add(item);
        return item;
    }
    
    protected override void OnInit()
    {
        Add(
            new CoinUpgradeItem()
                .Withkey("coin_percent_lv1")
                .WithDescription("金币掉落概率提升 Lv1")
                .WithPrice(100)
                .OnUpgrade((item) =>
                {
                    Global.CoinPercent.Value += 0.1f;
                    Global.Coin.Value -= item.Price;
                })
                .Next(Add(
                    new CoinUpgradeItem()
                        .Withkey("coin_percent_lv2")
                        .WithDescription("金币掉落概率提升 Lv2")
                        .WithPrice(800)
                        .OnUpgrade((item) =>
                        {
                            Global.CoinPercent.Value += 0.1f;
                            Global.Coin.Value -= item.Price;
                        })
                        .Next(Add(
                                new CoinUpgradeItem()
                                    .Withkey("coin_percent_lv3")
                                    .WithDescription("金币掉落概率提升 Lv3")
                                    .WithPrice(2000)
                                    .OnUpgrade((item) =>
                                    {
                                        Global.CoinPercent.Value += 0.1f;
                                        Global.Coin.Value -= item.Price;
                                    })
                                    .Next(Add(
                                        new CoinUpgradeItem()
                                            .Withkey("coin_percent_lv4")
                                            .WithDescription("金币掉落概率提升 Lv4")
                                            .WithPrice(5000)
                                            .OnUpgrade((item) =>
                                            {
                                                Global.CoinPercent.Value += 0.1f;
                                                Global.Coin.Value -= item.Price;
                                            })
                                    ))
                            )
                            )
                ))
        );
        
        // var coinPercentLv2 = 
        //
        // coinPercentLv1.OnChange.Register(() =>
        // {
        //     coinPercentLv2.OnChange.Trigger();
        // });
        //
        // var coinPercentLv3 = ;
        //
        // coinPercentLv2.OnChange.Register(() =>
        // {
        //     coinPercentLv3.OnChange.Trigger();
        // });
        
        
        Items.Add(
            new CoinUpgradeItem()
                .Withkey("exp_percent")
                .WithDescription("经验掉落概率提升")
                .WithPrice(6)
                .OnUpgrade((item) =>
                {
                    Global.ExpPercent.Value += 0.1f;
                    Global.Coin.Value -= item.Price;
                })
        );
        Items.Add(
            new CoinUpgradeItem()
                .Withkey("maxhp_percent")
                .WithDescription("血量上限概率提升")
                .WithPrice(10)
                .OnUpgrade((item) =>
                {
                    Global.MaxHP.Value++;
                    Global.Coin.Value -= item.Price;
                })
        );
        
        Load();

        OnCoinUpgradeSystemChanged.Register(() =>
        {
            Debug.Log("触发保存");
            Save();
        });
    }

    public void Save()
    {
        var saveSystem = this.GetSystem<SaveSystem>();
        foreach (var item in Items)
        {
            saveSystem.SaveBool(item.Key,item.UpgradeFinish);
        }
    }

    public void Load()
    {
        var saveSystem = this.GetSystem<SaveSystem>();
        foreach (var item in Items)
        {
            item.UpgradeFinish=saveSystem.LoadBool(item.Key, true);
        }
    }
}
